package com.qeeyou.apps.android.craftsman.activity.home;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.qeeyou.apps.android.craftsman.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xuchengxiong on 2018/2/6.
 */

public class MyGridViewAdapter extends BaseAdapter {
    private Context context;
    private List<String> itemList;

    public MyGridViewAdapter(Context context, List<String> itemList) {
        this.context = context;
        if(this.itemList == null) {
            this.itemList = new ArrayList<String>();
        }
        this.itemList.clear();
        if(itemList != null) {
            this.itemList.addAll(itemList);
        }
    }

    public void setItems(List<String> itemList) {
        if(this.itemList == null) {
            this.itemList = new ArrayList<String>();
        }
        this.itemList.clear();
        if(itemList != null) {
            this.itemList.addAll(itemList);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return itemList.size();
    }

    @Override
    public String getItem(int position) {
        return itemList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        View view = convertView;
        if(view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.view_item_grid_scheduling, null);
            holder.txtvName = (TextView) view.findViewById(R.id.txtvName4ItemGridScheduling);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        String name = itemList.get(position);
        holder.txtvName.setText(name);
        return view;
    }

    static class ViewHolder {
        private TextView txtvName;
    }
}
